package com.example.gestureprac

import android.os.Bundle
import android.content.Intent
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import com.example.gestureprac.repository.GestureRepository
import com.example.gestureprac.viewmodel.GestureViewModel
import com.example.gestureprac.viewmodel.GestureViewModelFactory

class MainActivity : AppCompatActivity() {
    
    private lateinit var gestureDetector: GestureDetectorCompat
    private lateinit var viewModel: GestureViewModel
    private lateinit var gestureText: TextView
    private lateinit var gestureHistoryText: TextView
    private lateinit var mainContainer: View
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        
        // 初始化视图
        mainContainer = findViewById(R.id.main)
        gestureText = findViewById(R.id.gesture_text)
        gestureHistoryText = findViewById(R.id.gesture_history)
        val slideActivityLink = findViewById<TextView>(R.id.slide_activity_link)
        
        // 设置点击事件监听器
        slideActivityLink.setOnClickListener {
            val intent = Intent(this, SlideGestureActivity::class.java)
            startActivity(intent)
        }
        
        // 初始化ViewModel
        val repository = GestureRepository()
        viewModel = ViewModelProvider(this, GestureViewModelFactory(repository))
            .get(GestureViewModel::class.java)
        
        // 设置WindowInsets监听器
        ViewCompat.setOnApplyWindowInsetsListener(mainContainer) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
        // 初始化手势检测器
        gestureDetector = GestureDetectorCompat(this, object : GestureDetector.SimpleOnGestureListener() {
            override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
                viewModel.onTap(e.x, e.y)
                return true
            }
            
            override fun onLongPress(e: MotionEvent) {
                viewModel.onLongPress(e.x, e.y)
            }
            
            override fun onDoubleTap(e: MotionEvent): Boolean {
                viewModel.onDoubleTap(e.x, e.y)
                return true
            }
        })
        
        // 监听容器的触摸事件
        mainContainer.setOnTouchListener { _, event ->
            gestureDetector.onTouchEvent(event)
            true
        }
        
        // 观察ViewModel中的数据变化
        observeViewModel()
        
        // 加载历史记录
        viewModel.loadGestureHistory()
    }
    
    /**
     * 观察ViewModel中的数据变化
     */
    private fun observeViewModel() {
        viewModel.currentGesture.observe(this) {
            it?.let { event ->
                val message = "${event.type}: (${event.x.toInt()}, ${event.y.toInt()})"
                gestureText.text = message
            }
        }

        viewModel.gestureHistory.observe(this) {
            val historyText = it.joinToString(separator = "\n") { event ->
                "${event.type}: (${event.x.toInt()}, ${event.y.toInt()})"
            }
            gestureHistoryText.text = historyText
        }
    }
}